use sea_orm_migration::prelude::*;

use entity::{resource, role_resource};

use crate::sea_orm::{ActiveModelTrait, NotSet};
use crate::sea_orm::ActiveValue::Set;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let db = manager.get_connection();
        resource::ActiveModel {
            id: Set(29),
            parent_id: Set(1),
            resource_name: Set(Some("字典分组管理".to_string())),
            resource_code: Set(Some("dict_group_manager".to_string())),
            resource_type: Set(Some(1)),
            resource_root: Set(Some(true)),
            resource_action: Set(Some(false)),
            order_number: Set(Some(0)),
            url: Set(Some("roleManager".to_string())),
            api_path: NotSet,
            api_http_method: Default::default(),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("字典分组管理菜单".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(30),
            parent_id: Set(29),
            resource_name: Set(Some("字典分组详情".to_string())),
            resource_code: Set(Some("api_dict_group_info".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict_group/info/".to_string())),
            api_http_method: Set(Some("GET".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("字典分组详情接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(31),
            parent_id: Set(29),
            resource_name: Set(Some("字典分组列表".to_string())),
            resource_code: Set(Some("api_dict_group_list".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict_group/list".to_string())),
            api_http_method: Set(Some("GET".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("字典分组列表接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(32),
            parent_id: Set(29),
            resource_name: Set(Some("字典分组分页查询".to_string())),
            resource_code: Set(Some("api_dict_group_page".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict_group/page".to_string())),
            api_http_method: Set(Some("POST".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("字典分组分页查询接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(33),
            parent_id: Set(29),
            resource_name: Set(Some("添加字典分组".to_string())),
            resource_code: Set(Some("api_dict_group_add".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict_group/add".to_string())),
            api_http_method: Set(Some("POST".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("添加字典分组接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(34),
            parent_id: Set(29),
            resource_name: Set(Some("修改字典分组".to_string())),
            resource_code: Set(Some("api_dict_group_update".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict_group/update".to_string())),
            api_http_method: Set(Some("POST".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("修改字典分组接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(35),
            parent_id: Set(29),
            resource_name: Set(Some("删除字典分组".to_string())),
            resource_code: Set(Some("api_dict_group_delete".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict_group/delete".to_string())),
            api_http_method: Set(Some("GET".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("删除字典分组接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(36),
            parent_id: Set(1),
            resource_name: Set(Some("字典管理".to_string())),
            resource_code: Set(Some("dict_manager".to_string())),
            resource_type: Set(Some(1)),
            resource_root: Set(Some(true)),
            resource_action: Set(Some(false)),
            order_number: Set(Some(0)),
            url: Set(Some("roleManager".to_string())),
            api_path: NotSet,
            api_http_method: Default::default(),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("字典管理菜单".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(37),
            parent_id: Set(36),
            resource_name: Set(Some("字典详情".to_string())),
            resource_code: Set(Some("api_dict_info".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict/info/".to_string())),
            api_http_method: Set(Some("GET".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("字典详情接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(38),
            parent_id: Set(36),
            resource_name: Set(Some("字典列表".to_string())),
            resource_code: Set(Some("api_dict_list".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict/list".to_string())),
            api_http_method: Set(Some("GET".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("字典列表接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(39),
            parent_id: Set(36),
            resource_name: Set(Some("字典分页查询".to_string())),
            resource_code: Set(Some("api_dict_page".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict/page".to_string())),
            api_http_method: Set(Some("POST".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("字典分页查询接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(40),
            parent_id: Set(36),
            resource_name: Set(Some("添加字典".to_string())),
            resource_code: Set(Some("api_dict_add".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict/add".to_string())),
            api_http_method: Set(Some("POST".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("添加字典接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(41),
            parent_id: Set(36),
            resource_name: Set(Some("修改字典".to_string())),
            resource_code: Set(Some("api_dict_update".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict/update".to_string())),
            api_http_method: Set(Some("POST".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("修改字典接口".to_string())),
        }
        .insert(db)
        .await?;
        resource::ActiveModel {
            id: Set(42),
            parent_id: Set(36),
            resource_name: Set(Some("删除字典".to_string())),
            resource_code: Set(Some("api_dict_delete".to_string())),
            resource_type: Set(Some(2)),
            resource_root: Set(Some(false)),
            resource_action: Set(Some(true)),
            order_number: Set(Some(0)),
            url: Default::default(),
            api_path: Set(Some("/api/dict/delete".to_string())),
            api_http_method: Set(Some("GET".to_string())),
            role: NotSet,
            status: Set(Some(true)),
            icon: NotSet,
            resource_desc: Set(Some("删除字典接口".to_string())),
        }
        .insert(db)
        .await?;
        for id in 29..43 {
            role_resource::ActiveModel {
                id: NotSet,
                role_id: Set(1),
                resource_id: Set(id),
            }
            .insert(db)
            .await?;
        }
        Ok(())
    }

    async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
        Ok(())
    }
}
